package com.game.ui
{
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.Stage;

	public class PopUpManager
	{
		private static var container : DisplayObjectContainer;
		private static var popUps : Array = [];
		
		public static function init(mainContainer : DisplayObjectContainer) : void
		{
			container = mainContainer;
		}
		
		public static function addPop(value : DisplayObjectContainer , parent : DisplayObjectContainer = null , index : int = -1):void
		{
			if(parent == null)
			{
				parent = container;
			}
			
			if(index == -1)
			{
				parent.addChild(value);
			}else
			{
				parent.addChildAt(value , index);
			}
			popUps.push(value);
		}
		
		public static function removePop(value : DisplayObjectContainer) : void
		{
			var index : int = popUps.indexOf(value);
			if(index > -1)
				popUps.splice(index,1);
			container.removeChild(value);
		}
		
		public static function centerPopUp(popUp : DisplayObject) : void
		{
			if(popUp.parent == container)
			{
				var w : int = popUp.parent is Stage ? (popUp.parent as Stage).stageWidth : popUp.parent.stage.stageWidth
				var h : int = popUp.parent is Stage ? (popUp.parent as Stage).stageHeight : popUp.parent.stage.stageHeight;
			}
			else
			{
				w = popUp.parent is Stage ? (popUp.parent as Stage).stageWidth : popUp.parent.width;
				h = popUp.parent is Stage ? (popUp.parent as Stage).stageHeight : popUp.parent.height;
			}
			
			popUp.x = (w - popUp.width) / 2;
			popUp.y = (h - popUp.height) / 2;
		}
		
		public function PopUpManager()
		{
		}
	}
}